package com.yhc.my.an_meishia.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.yhc.my.an_meishia.R;
import com.yhc.my.an_meishia.bean.Users;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class RegisterActivity extends Activity  {

    private EditText etName;
    private EditText etPass;
    private String names;
    private String pass;
    private BmobUser bmobUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        bmobUser = new BmobUser();
    }

    private void initView() {
        etName = (EditText) findViewById(R.id.et_name);
        etPass = (EditText) findViewById(R.id.et_pass);

    }

    //登录
    public void register(View view) {
        //输入的数据
        names = etName.getText().toString();
        pass = etPass.getText().toString();
        // 非空验证
        if(TextUtils.isEmpty(names) || TextUtils.isEmpty(pass)){
            Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
//        bmobUser.setUsername(names);
//        bmobUser.setPassword(pass);
        BmobQuery<Users> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereLessThanOrEqualTo("phoneNumber",names);
        BmobQuery<Users> bmobQuery1 = new BmobQuery<>();
        bmobQuery1.addWhereLessThanOrEqualTo("password",pass);
        List<BmobQuery<Users>> queries=new ArrayList<>();
        queries.add(bmobQuery);
        queries.add(bmobQuery1);
        BmobQuery<Users> query = new BmobQuery<>();
        query.and(queries);
        query.findObjects(new FindListener<Users>() {
            @Override
            public void done(List<Users> list, BmobException e) {
                if(e==null){
                    Toast.makeText(RegisterActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(RegisterActivity.this,"登录失败，请检查用户名或密码",Toast.LENGTH_SHORT).show();
                }
            }
        });
//        bmobUser.login(new SaveListener<BmobUser>() {
//            @Override
//            public void done(BmobUser o, BmobException e) {
//                if (e==null){
//                    Toast.makeText(RegisterActivity.this,"ok",Toast.LENGTH_SHORT).show();
//                }else {
//                    Toast.makeText(RegisterActivity.this,"no",Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
   }

    public void login(View view) {
        startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
    }
}
